﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace quizzer_linq_3
{
    public partial class QuestionsAndAnswers : Form
    {
        public QuestionsAndAnswers(int gameset)
        {
            InitializeComponent();
            populateQuestionsFromGameSet(gameset);
        }

        public QuestionsAndAnswers(string professor, string subcategory)
        {
            InitializeComponent();
            populateQuestionsFromCategory(subcategory, professor);
        }

        private void comboBoxQuestions_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DataAccess is the variable for using the database

            Quizzer_Data_Access dataAccess = new Quizzer_Data_Access();

            //Gets the question from the combo box

            String question = comboBoxQuestions.Text.Trim();

            //Gets all the questions

            String[] getAllQuestions = dataAccess.GetAllQuestion().Split('\n');

            //Populates the answer box

            for (int counter = 0; counter < getAllQuestions.Length - 1; counter++)
            {
                String[] localQuestion = ((String)getAllQuestions.GetValue(counter)).Split('|');
                if (localQuestion.GetValue(3).Equals(question))
                {
                    comboBoxAnswers.Items.Add(localQuestion.GetValue(6));
                    comboBoxAnswers.Items.Add(localQuestion.GetValue(7));
                    comboBoxAnswers.Items.Add(localQuestion.GetValue(8));
                    comboBoxAnswers.Items.Add(localQuestion.GetValue(9));
                    comboBoxAnswers.Items.Add(localQuestion.GetValue(10));
                }
            }

        }

        private void populateQuestionsFromGameSet(int gID)
        {
            //DataAccess is the variable for using the database

            Quizzer_Data_Access dataAccess = new Quizzer_Data_Access();

            //Gets all the questionID's from a gameset

            String[] allQuestionsID = dataAccess.questionsFromGameset(gID).Split('\n');

            //Gets all the questions and puts them in the box

            for (int counter = 0; counter > allQuestionsID.Length - 1; counter++)
            {
                String[] allID = ((String)allQuestionsID.GetValue(counter)).Split('|');
                comboBoxQuestions.Items.Add(dataAccess.GetQuestion(int.Parse(((String)allID.GetValue(0)).Trim())).Split('|').GetValue(3));
            }

        }

        private void populateQuestionsFromCategory(string subcategory, string professor)
        {
            //DataAccess is the variable for using the database

            Quizzer_Data_Access dataAccess = new Quizzer_Data_Access();

            //Gets the subcategory ID

            int subID = int.Parse(dataAccess.GetSubCategory(subcategory).Split('|').GetValue(0).ToString());

            //Gets all the questions

            String[] allQuestions = dataAccess.GetAllQuestion().Split('\n');

            //This is suppose to use the professor email to lookup the professor ID but that function is not available in the database...

            int pID = 1000;

            //Sorts out the questions and puts them into the combo box

            for (int counter = 0; counter < allQuestions.Length - 1; counter++)
            {
                String[] question = ((String)allQuestions.GetValue(counter)).Split('|');
                if (int.Parse(question.GetValue(1).ToString()) == subID && int.Parse(question.GetValue(13).ToString()) == pID)
                {
                    comboBoxQuestions.Items.Add(question.GetValue(3));
                }
            }
        }

        /** Method: buttonBack_Click
         * Closes this form
         * 
         * @param sender sender of the action
         * @param e event is triggered
        */

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
